<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 收藏 model
 *
 */
class Collectionmodel extends CI_Model {

	function __construct()
    {
        parent::__construct();
    }
    
    /**
     * 获取会员的收藏信息
     *
     * @param unknown_type $uid
     */
    function get_collections($uid = '')
    {
    	$result = array();
    	$this->dblib->set_query_limit();
    	$this->db->select('c.*, a.name as appname, u.id, u.name')
				->from('collection as c')
				->join('app as a', 'c.appid=a.id')
				->join('user as u', 'u.id=a.uid')
				->where('c.uid', $uid)
				->order_by('c.createtime', 'desc');
		$query = $this->db->get();
		if($query->num_rows() > 0){
			//如果有结果集
			$result = $query->result_array();
		}
		return $result;
    }
    
    /**
     * 获取具体一条收藏信息
     *
     * @param unknown_type $id
     */
    function get_collection($id='')
    {
    	
    }
    
    /**
     * 检测给定的应用具体用户是否收藏过
     *
     * @param unknown_type $appid
     * @param unknown_type $uid
     * @param unknown_type $collection_type 收藏类型 默认为1 即收藏的是应用
     * @return unknown
     */
    function check_collection($appid='', $uid='', $collection_type=1)
    {
    	$this->db->select('count(*) as count');
		$query = $this->db->get_where('collection', array('collection_id' => $appid, 'uid' => $uid, 'collection_type' => $collection_type));
		$row = $query->row_array();
		return $row['count'] > 0;
    }
    
    /**
     * 检测给定的收藏和给定的用户是否匹配
     *
     * @param unknown_type $id
     * @param unknown_type $uid
     */
    function check_collection_2($id='', $uid='')
    {
    	$this->db->select('count(*) as count');
		$query = $this->db->get_where('collection', array('id' => $id, 'uid' => $uid));
		$row = $query->row_array();
		return $row['count'] > 0;
    }
    
    /**
     * 添加收藏
     *
     * @param unknown_type $uid
     * @param unknown_type $appid
     */
    function insert_collection($uid='', $appid='', $type=1)
    {
    	//生成id
    	$this->db->set('id', '(SELECT UUID())', FALSE);
    	$this->db->set('createtime', '(SELECT NOW())', FALSE);
    	$data = array('uid'	=> $uid, 'collection_id'	=> $appid, 'collection_type' => $type);
		return $this->db->insert('collection', $data);
    }
    
    /**
     * 删除收藏
     *
     * @param unknown_type $id
     */
    function delete_collection($id='')
    {
    	return $this->db->delete('collection', array('id'=>$id));
    }
    
}

/* End of file collectionsmodel.php */
/* Location: ./application/models/collectionsmodel.php */